package com.sc.nft.controller;


import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.dto.MyGuessDTO;
import com.sc.nft.entity.dto.NumberQueryListDTO;
import com.sc.nft.entity.dto.UserBettingDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.UserPrizeDrawRecordsService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户抽奖号码表(UserPrizeDrawRecords)表控制层
 *
 * @author 王伟成
 * @since 2023-09-14 15:41:56
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("userPrizeDrawRecords")
@Api(tags = "用户参与竞猜")
public class UserPrizeDrawRecordsController {
    /**
     * 服务对象
     */
    private final UserPrizeDrawRecordsService userPrizeDrawRecordsService;

    private final RedissonClient redissonClient;

    @ApiOperation(value = "预创建验证", notes = "预创建验证", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/creationVerify", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<String>> creationVerify(@Valid @RequestBody UserBettingDTO userBettingDTO)  {
        return Result.success(userPrizeDrawRecordsService.creationVerify(userBettingDTO, UserPool.getUser()));
    }

    @ApiOperation(value = "用户下注", notes = "用户下注", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userBetting", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result userBetting(@Valid @RequestBody UserBettingDTO userBettingDTO)  {
        String key = StrUtil.format("{}:{}", "userBetting", UserPool.getUser().getId());
        RLock lock = redissonClient.getLock(key);
        boolean b = lock.tryLock();
        if (!b) {
            throw new GlobalRunTimeException("请求频次过高");
        }
        try{
            userPrizeDrawRecordsService.userBetting(userBettingDTO, UserPool.getUser());
        }finally {
            lock.unlock();
        }
        return Result.success();
    }


    @ApiOperation(value = "我的竞猜(竞猜)/我的抽奖号码(瓜分)", notes = "我的竞猜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myGuess", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyGuessVO> myGuess(@Valid @RequestBody MyGuessDTO myGuessDTO)  {
        return PageResult.success(userPrizeDrawRecordsService.myGuess(myGuessDTO, UserPool.getUser()),MyGuessVO.class);
    }
    @ApiOperation(value = "我的号码", notes = "我的号码", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myNumber", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MyNumberVO> myNumber(@Valid @RequestBody IdRequest request)  {
        return Result.success(userPrizeDrawRecordsService.myNumber(request.getId()));
    }




    @ApiOperation(value = "中奖名单", notes = "中奖名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/listWinners", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<WinnersListVO>> listWinners(@Valid @RequestBody IdRequest idRequest)  {
        return Result.success(userPrizeDrawRecordsService.listWinners(idRequest.getId()));
    }

    @ApiOperation(value = "编号查询", notes = "中奖名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/numberQueryList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<NumberQueryListVO>> numberQueryList(@Valid @RequestBody NumberQueryListDTO numberQueryListDTO)  {
        return Result.success(userPrizeDrawRecordsService.numberQueryList(numberQueryListDTO));
    }
    @ApiOperation(value = "我的中奖名单(瓜分)", notes = "我的中奖名单(瓜分)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myListWinners", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<MyListWinnersVO>> myListWinners(@Valid @RequestBody IdRequest idRequest){
        return Result.success(userPrizeDrawRecordsService.myListWinners(idRequest.getId(),UserPool.getUser()));
    }

    @ApiOperation(value = "中奖总名单(瓜分)", notes = "中奖总名单(瓜分))", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/listOfWinners", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<ListOfWinnersVO>> listOfWinners(@Valid @RequestBody IdRequest idRequest){
        return Result.success(userPrizeDrawRecordsService.listOfWinners(idRequest.getId()));
    }

}
